package com.ly.hotel.view.fragments.youhui;

import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;

import com.ly.hotel.R;
import com.ly.hotel.model.YouhuiMessage;

import java.util.List;

public class YouhuiAdapter extends PagerAdapter {
    private List<YouhuiMessage> youhuiMsg;

    public YouhuiAdapter(List<YouhuiMessage> dataList) {
        youhuiMsg = dataList;
    }

    @Override
    public int getCount() {
        return youhuiMsg == null ? 0 : youhuiMsg.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int pd = container.getContext().getResources().getDimensionPixelOffset(R.dimen.px10);
        LinearLayout linearLayout = new LinearLayout(container.getContext());
        linearLayout.setGravity(Gravity.CENTER);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(layoutParams);
        container.addView(linearLayout);

        ScrollView scrollView = new ScrollView(container.getContext());

        TextView textView = new TextView(container.getContext());
        textView.setText(Html.fromHtml(youhuiMsg.get(position).getContent()));
        textView.setPadding(pd,pd,pd,pd);
        textView.setLineSpacing(pd,1);

        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,container.getContext().getResources().getDimensionPixelSize(R.dimen.px30));
        scrollView.addView(textView);
        linearLayout.addView(scrollView);
        return linearLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return youhuiMsg.get(position).getTitle();
    }
}